﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Subsonic.Data;
using Microsoft.Phone.Tasks;
using MichMan.Utilities;
using MichMan.Utilities.Requests;
using Artist = Subsonic.Data.Artist;
using System.Collections.ObjectModel;
using Subsonic.Resources;
using System.IO.IsolatedStorage;

namespace Subsonic.ViewModels
{
    public static class NavigationServiceExtensions
    {
        public static void Navigate(this NavigationService ns, ItemViewModel item)
        {
            ns.Navigate(UriInferer.InferUri(item));
        }
    }

    public class MainPivotItem : ItemViewModel
    {
        public MainPivotItem()
        {
            LineOne = "NEW UI";
            Link = "/Pages/MainPivot.xaml";
        }
    }

    public class SongsToPreLoadItem : ChoiceItem
    {
        public SongsToPreLoadItem()
            : base(typeof(SongsToPreLoadChoice).FullName)
        {
        }
    }

    public class CacheSizeItem : ChoiceItem
    {
        public CacheSizeItem()
            : base(typeof(CacheSizeChoice).FullName)
        {
        }
    }

    public class ClearCache : ItemViewModel
    {
        public ClearCache()
        {
            LineOne = "{cache.clear}";
        }

        public override void DoAction(NavigationService navService)
        {
            new LocalCache().Clear();
        }
    }

    public class PlayModeItem : ChoiceItem
    {
        public PlayModeItem()
            : base(typeof(PlayModeChoice).FullName)
        {
        }
    }

    public class WiFiBitRateItem : ChoiceItem
    {
        public WiFiBitRateItem()
            : base(typeof(WiFiBitRateChoice).FullName)
        {
        }
    }

    public class MobileBitRateItem : ChoiceItem
    {
        public MobileBitRateItem()
            : base(typeof(MobileBitRateChoice).FullName)
        {
        }
    }

    public class ChoiceItem : ItemViewModel
    {
        public ChoiceItem(string choiceType)
        {
            ChoiceType = choiceType;
            Link = "/Pages/ChoicePage.xaml";
            Choice choice = Choice.GetChoice(choiceType);
            LineOne = choice.Title;
            LineTwo = choice.Values[choice.SelectedItem ];
        }

        [QueryParam("choiceType", Required = true)]
        public string ChoiceType { get; protected set; }
    }

    public class Serveritem : ItemViewModel
    {
        public Serveritem(int index)
        {
            Index = index;
            Link = "/Pages/ServerSettingsPage.xaml";

            index = Math.Max(0, index);
            index = Math.Min(index, SubsonicSettings.Instance.Servers.Count-1);

            SubsonicServer server = SubsonicSettings.Instance.Servers[index];
            LineOne = server.Name;
            LineTwo = server.ServerAddress;
        }

        [QueryParam("index", Required=true)]
        public int Index { get; set; }
    }

    public class SelectServerItem : ChoiceItem
    {
        public SelectServerItem()
            : base(typeof(ServerChoice).FullName)
        {
        }
    }

    public class SettingsItem : ItemViewModel
    {
        public SettingsItem()
        {
            LineOne = "{main_settings}";
            Link = "/Pages/SettingsPage.xaml";
        }
    }

    public class HelpItem : ItemViewModel
    {
        public HelpItem()
        {
            LineOne = "{help_label}";
            Link = "/Pages/HelpPage.xaml";
        }
    }

    public class QueueItem : ChildItem
    {
        public QueueItem(Child child) : base(child) { }
    }

    public class AlbumListChildItem : ChildItem
    {
        public AlbumListChildItem(AlbumListType albumListType, Child child) : base(child)
        {
            if (child.IsDir)
            {
                LineOne = child.Title;
                Link = String.Format("/Pages/AlbumsPivot.xaml?albumId={0}&album={1}&albumListType={2}", child.Id, child.Title, albumListType.ToString());
            }
        }
    }

    public class ChildItem : ItemViewModel
    {
        public ChildItem(Child child)
        {
            Id = child.Id;
            Child = child;
            LineOne = Child.Title;

            if (!Child.IsDir)
            {
                Link = "/Pages/PlayQueue.xaml";
            }
            
            ItemImage = SubsonicRequestContext.CoverArtUrl(child.CoverArt);
        }

        public Child Child { get; private set; }

        public override void DoAction(NavigationService navService)
        {
            if (Child.IsDir)
            {
                base.DoAction(navService);
            }
        }
    }

    public class ArtistItem : ItemViewModel
    {
        public ArtistItem(Artist artist)
        {
            Artist = artist;
            LineOne = Artist.Name;
            Link = String.Format("/Pages/ArtistPivotPage.xaml?id={0}&artist={1}", Artist.Id, Artist.Name);
        }

        public Artist Artist { get; private set; }
    }

    public class AlbumItem : ItemViewModel
    {
        public AlbumItem(Artist artist, Child album)
        {
            Album = album;
            Artist = artist;
            LineOne = Album.Title;
            Link = String.Format("/Pages/AlbumsPivot.xaml?artistId={0}&artist={1}&albumId={2}", Artist.Id, Artist.Name, Album.Id);
            ItemImage = SubsonicRequestContext.CoverArtUrl(album.CoverArt);
        }

        public Artist Artist { get; private set; }
        public Child Album { get; private set; }
    }

    [UriFormat("{link}", UriKind.Relative)]
    public class ItemViewModel : BaseViewModel
    {
        public ItemViewModel()
        {
        }

        public virtual void DoAction(NavigationService navService) 
        {
            Uri uri = UriInferer.InferUri(this);
            if (uri != null)
            {
                navService.Navigate(uri);
            }
        }

        [UriParam("link")]
        public virtual string Link { get; protected set; }

        public Visibility ItemImageVisibility
        {
            get
            {
                return ItemImage != null ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        private string itemImage = null; //"/Subsonic;component/Images/ArrowImg.png";
        public string ItemImage
        {
            get
            {
                return itemImage;
            }
            set
            {
                if (value != itemImage)
                {
                    itemImage = value;
                    NotifyPropertyChanged("ItemImage");
                    NotifyPropertyChanged("ItemImageVisiblity");
                }
            }

        }

        private string lineOne;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string LineOne
        {
            get
            {
                return lineOne;
            }
            set
            {
                if (value != lineOne)
                {
                    lineOne = value.StaticResource();
                    NotifyPropertyChanged("LineOne");
                }
            }
        }

        private string lineTwo;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string LineTwo
        {
            get
            {
                return lineTwo;
            }
            set
            {
                if (value != lineTwo)
                {
                    lineTwo = value.StaticResource();
                    NotifyPropertyChanged("LineTwo");
                }
            }
        }

        private string lineThree;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string LineThree
        {
            get
            {
                return lineThree;
            }
            set
            {
                if (value != lineThree)
                {
                    lineThree = value.StaticResource();
                    NotifyPropertyChanged("LineThree");
                }
            }
        }
    }
}